主题
HTTP高级请求 - HttpRequestEx
函数简介
高级HTTP请求,支持自定义Method、请求头(含Cookie)、请求体。
接口名称
HttpRequestExDLL调用
c
const char* HttpRequestEx(int64_t instance, const char* method, const char* url, const char* headers, const char* body, const char* content_type, int32_t* status_code);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| method | 字符串 | 方法,如"GET"/"POST"/"PUT"/"DELETE",大小写不敏感。 |
| url | 字符串 | 完整URL。 |
| headers | 字符串 | 自定义请求头,多行字符串,每行"Name: Value",可为空。 |
| body | 字符串 | 请求体,GET可传空。 |
| content_type | 字符串 | 如"application/json",可为空。 |
| status_code | 整数型指针 | 输出HTTP状态码,可为NULL。 |
使用场景
| 场景 | method | 关键参数 |
|---|---|---|
| Bearer Token 鉴权 | GET/POST | headers 含 Authorization: Bearer xxx |
| 带 Cookie 请求 | GET/POST | headers 含 Cookie: session=abc |
| REST 更新/删除 | PUT/DELETE | body + content_type |
| 需要判断状态码 | 任意 | 传入 status_code 输出参数 |
headers 格式:每行 Name: Value,以 \r\n 分隔。示例:
Authorization: Bearer your_token_here
User-Agent: OLAPlug/1.0
Accept: application/json完整示例见 网络使用指南。
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// 自定义 Header(含 Bearer Token)
string headers =
"Authorization: Bearer your_token_here\r\n"
"User-Agent: OLAPlug/1.0\r\n"
"Accept: application/json";
int status = 0;
string resp = ola.HttpRequestEx(
"GET",
"https://api.example.com/v1/user/profile",
headers, "", "", status
);
// status 为 HTTP 状态码(200/401/404 等)csharp
using OLAPlug;
var ola = new OLAPlugServer();
string headers = "Authorization: Bearer your_token_here\r\nUser-Agent: OLAPlug/1.0";
int status = 0;
string resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", headers, "", "", status);python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
headers = "Authorization: Bearer your_token_here\r\nUser-Agent: OLAPlug/1.0"
status = 0
resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", headers, "", "", status)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
String headers = "Authorization: Bearer your_token_here\r\nUser-Agent: OLAPlug/1.0";
int status = 0;
String resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", headers, "", "", status);go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
status := 0
resp := ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status)rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let status = 0;
let resp = ola.http_request_ex("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", &status);cpp
var ola = com("OlaPlug.OlaSoft")
var status = 0
var resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status)vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
status = 0
resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status)text
.局部变量 ola, OLAPlug
ola.创建 ()
status = 0
resp = ola.HttpRequestEx ("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token", "", "", status)aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var status = 0;
var resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
整数 status = 0
文本型 resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token", "", "", status)cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
int status = 0;
string resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status);原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
int status = 0;
long respPtr = HttpRequestEx(instance, "GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status);
if (respPtr != 0) FreeStringPtr(respPtr);csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long HttpRequestEx(long ola, string method, string url, string headers, string body, string content_type, ref int status_code);
long instance = CreateCOLAPlugInterFace();
int status = 0;
long respPtr = HttpRequestEx(instance, "GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status);
if (respPtr != 0) FreeStringPtr(respPtr);python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
int status = 0;
long respPtr = HttpRequestEx(instance, "GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status);
if (respPtr != 0) FreeStringPtr(respPtr);返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 字符串指针,返回响应体内容,失败返回NULL。需调用 FreeStringPtr 释放内存。 |
注意事项
| 项目 | 说明 |
|---|---|
| 释放内存 | 返回的字符串需要调用 FreeStringPtr 释放内存。 |
| headers格式 | 每行"Name: Value",以"\r\n"分隔。 |
| 支持的HTTP方法 | GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS等。 |
| status_code可以为NULL | status_code可以为NULL,如果不需要获取状态码。 |
